package com.wsq.demo01.controller;

import com.alibaba.fastjson.JSONObject;
import com.wsq.demo01.mapper.StudentMapper;
import com.wsq.demo01.pojo.StudentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class StudentController {

    private StudentMapper studentMapper;

    @Autowired
    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    /*测试项目是否正常启动*/
    @GetMapping("/test1")
    public String test1() {
        return "test1 is success";
    }

    /*
     * 重置数据表
     * */
    @GetMapping("/init")
    public String init() {
        int i = studentMapper.truncateTable();
        if (i == 0) {
            System.out.println("student_info表：已清空");
            StudentInfo studentInfo1 = new StudentInfo(1, "张三", "12345678", "软件工程系", "卓越2班", "男");
            StudentInfo studentInfo2 = new StudentInfo(2, "李四", "22222222", "软件工程系", "卓越1班", "女");
            StudentInfo studentInfo3 = new StudentInfo(3, "王五", "11122233", "软件工程系", "卓越4班", "男");
            studentMapper.addStudentInfo(studentInfo1);
            studentMapper.addStudentInfo(studentInfo2);
            studentMapper.addStudentInfo(studentInfo3);
            return "student_info表：重置成功";
        }
        return "重置失败";
    }

    /*查询学生全部信息*/
    @GetMapping("/list")
    public List<StudentInfo> queryList() {
        List<StudentInfo> studentInfos = studentMapper.queryStudentList();
        for (StudentInfo studentInfo : studentInfos) {
            System.out.println(studentInfo);
        }
        return studentInfos;
    }

    /*通过名字查询学生信息*/
    @GetMapping("/byName")
    public String queryByName(@RequestParam String name) {
        if (studentMapper.findUserByName(name) != null) {
            return studentMapper.findUserByName(name).toString();
        }
        return "user is not exists";
    }

    /*body传参——post:add*/
    @PostMapping("/body/insert")
    public String bodyInsert(@RequestBody JSONObject jsonObject) {
        JSONObject jsonList = jsonObject.getJSONObject("info");
        StudentInfo studentInfo = JSONObject.parseObject(jsonList.toString(), StudentInfo.class);
        Integer id = Math.toIntExact(studentInfo.getId());
        if (studentMapper.findUserById(id) == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("name", studentInfo.getName());
            map.put("Stu_id", studentInfo.getStu_id());
            map.put("Depart", studentInfo.getDepart());
            map.put("grade", studentInfo.getGrade());
            map.put("sex", studentInfo.getSex());
            studentMapper.testAdd(map);
            return "add is success";
        }
        return "add is fail";
    }

    /*body传参——post:update*/
    @PostMapping("/body/update")
    public String bodyUpdate(@RequestBody JSONObject jsonObject) {
        JSONObject jsonList = jsonObject.getJSONObject("info");
        StudentInfo studentInfo = JSONObject.parseObject(jsonList.toString(), StudentInfo.class);
        String name = studentInfo.getName();
        if (studentMapper.findUserByName(name) != null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", name);
            map.put("grade", studentInfo.getGrade());
            studentMapper.testUpdate(map);
            return "update is success";
        }
        return "update is fail";
    }

    /*body传参——post:delete*/
    @PostMapping("/body/delete")
    public String bodyDelete(@RequestBody JSONObject jsonObject) {
        JSONObject jsonList = jsonObject.getJSONObject("info");
        StudentInfo studentInfo = JSONObject.parseObject(jsonList.toString(), StudentInfo.class);
        String name = studentInfo.getName();
        if (studentMapper.findUserByName(name) != null) {
            studentMapper.deleteStudent(name);
            return "delete is success";
        }
        return "delete is fail";
    }

}
